package com.itheima.project.knowledge;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.gson.Gson;
import com.itheima.project.*;
import com.itheima.project.constant.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class FragmentKnowledge extends Fragment {

    private RecyclerView rvContent;
    private List<KnowledgeTreeEntity.DataBean> mKnowledgList = new ArrayList<>();
    private KnowledgeAdapter knowledgeAdapter;
    private int mCurrentPage = 0;
    private SwipeRefreshLayout srlKnowledge;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.knowledge_fragment, null, false);
        rvContent = inflate.findViewById(R.id.rv_content_knowledge_fragment);
        srlKnowledge = inflate.findViewById(R.id.srl_knowledge_fragment);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initEvent();
    }

    private void initData() {
        rvContent.setLayoutManager(new LinearLayoutManager(getContext()));
        knowledgeAdapter = new KnowledgeAdapter(getContext(), mKnowledgList);
        rvContent.setAdapter(knowledgeAdapter);
        requestKnowledge();
    }

    private void requestKnowledge() {
        OkHttpUtil.enqueue(ApiInterface.KNOWLEDGE_URL, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                KnowledgeTreeEntity knowledgeEntity = new Gson().fromJson(string, KnowledgeTreeEntity.class);
                if (knowledgeEntity.getErrorCode() == 0) {
                    mKnowledgList.clear();
                    mKnowledgList.addAll(knowledgeEntity.getData());
                    HandlerUtil.post(new Runnable() {
                        @Override
                        public void run() {
                            knowledgeAdapter.notifyDataSetChanged();
                            srlKnowledge.setRefreshing(false);
                        }
                    });
                }

            }
        });
    }

    private void initEvent() {
        srlKnowledge.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                requestKnowledge();
            }
        });
        KnowledgeAdapter.OnKnowledgeItemListener knowledgeItemListener = new KnowledgeAdapter.OnKnowledgeItemListener() {
            @Override
            public void OnKnowledgeClick(int position, KnowledgeTreeEntity.DataBean dataBean) {
                String name = dataBean.getName();
                Toast.makeText(getContext(),name,Toast.LENGTH_SHORT).show();
                SubPageActivity.startSubPageActivity(getContext(),name,dataBean.getChildren());
            }
        };
        knowledgeAdapter.setOnKnowledgeItemListener(knowledgeItemListener);
    }
    public void scrollToTop(){
        rvContent.scrollToPosition(0);
    }
}
